package com.loan.web.api;


import com.loan.entity.Menu;
import com.loan.entity.Perm;
import com.loan.entity.ResponseResult;
import com.loan.service.MenuService;
import com.loan.vo.MenuVO;
import com.loan.vo.PermVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.xml.ws.soap.Addressing;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;


    @GetMapping("getmenuall")
    public ResponseResult<List<MenuVO>> getall(){
        List<Menu> getall = menuService.getall();
        return new ResponseResult(getall);
    }

    @GetMapping("getall")
    public ResponseResult<List<MenuVO>> getAll(){
        //菜单，子菜单列表

        List<Menu> allMenu = menuService.getAll();
        List<MenuVO> menuVOS = new ArrayList<>();

        allMenu.forEach( p->{
            if(p.getSubMenu() == 0){
                MenuVO menuVO = new MenuVO();
                menuVO.setId(p.getId());
                menuVO.setLabel(p.getName());
                menuVO.setChildren(new ArrayList<MenuVO>());
                menuVOS.add(menuVO);
            }
        });

        menuVOS.forEach(mvo->{
            allMenu.forEach(p->{
                if(p.getSubMenu() == mvo.getId()){
                    MenuVO menuVO = new MenuVO();
                    menuVO.setId(p.getId());
                    menuVO.setLabel(p.getName());
                    mvo.getChildren().add(menuVO);
                }
            });
        });

        return new ResponseResult(menuVOS);
    }


}
